package com.xof.phonecall

import android.view.View
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.xof.phonecall.base.BaseActivity
import com.xof.phonecall.databinding.ActivityLoginBinding
import com.xof.phonecall.extipetrpide.startToNextActivityAndFinish
import com.xof.phonecall.model.ToDoLoginTo
import com.xof.phonecall.valipetrpide.ValPoint

class LoginActivity : BaseActivity() {

    lateinit var mActivityBinding: ActivityLoginBinding

    private val mViewModelLaunch by lazy {
        ViewModelProvider(this).get(ViewModelLaunch::class.java)
    }

    private val mViewModelLogin by lazy {
        ViewModelProvider(this).get(ViewModelLogin::class.java)
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mViewModelLaunch)
        list.add(mViewModelLogin)
        return list
    }

    override fun onBindingViewTag(): String {
        return ValPoint.LOGIN.name
    }

    override fun onBindingContentView(): View {
        mActivityBinding = ActivityLoginBinding.inflate(layoutInflater)
        return mActivityBinding.root

    }

    override fun onContentViewBinded() {
        try {
            //登录返回
            mViewModelLogin.LiveDataDoLogin.observe(this) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { result ->
                        //转到电话页
                        val hashMap = HashMap<String, String>()
                        startToNextActivityAndFinish<OutgoingCallActivity>(params = hashMap)
                    }
                }
            }

            //去登录
            mActivityBinding.textLogin.setOnClickListener {
                if (isAllFormDataCompleted()) {
                    if (MyApplication.IS_DEBUG) {
                        //测试
                        val hashMap = HashMap<String, String>()
                        startToNextActivityAndFinish<OutgoingCallActivity>(params = hashMap)
                    } else {
                        val account = mActivityBinding.editAccountInput.text.toString().trim()
                        val password = mActivityBinding.editPasswordInput.text.toString().trim()
                        mViewModelLogin.doLogin(
                            ToDoLoginTo(
                                loginName = account,
                                passWord = password
                            )
                        )
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private fun isAllFormDataCompleted(): Boolean {
        val account = mActivityBinding.editAccountInput.text.toString().trim()
        val password = mActivityBinding.editPasswordInput.text.toString().trim()
        if (account.isBlank()) {
            Toast.makeText(this, "Please input your Account!", Toast.LENGTH_SHORT).show()
            return false
        }
        if (password.isBlank()) {
            Toast.makeText(this, "Please input your Password!", Toast.LENGTH_SHORT).show()
            return false
        }
        return true
    }
}